<template>
  <div id="app">
    <a-form :model="searchParams">
      <a-row :gutter="16">
        <a-col :span="8">
          <a-form-item field="value1" label="标题" label-col-flex="100px">
            <a-input
              v-model="searchParams.title"
              placeholder="please enter..."
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="value2" label="描述" label-col-flex="80px">
            <a-input
              v-model="searchParams.content"
              placeholder="please enter..."
            />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item field="value3" label="标签" label-col-flex="80px">
            <a-input-tag
              v-model="searchParams.tags"
              :style="{ width: '320px' }"
              placeholder="按Enter键入题目标签"
              allow-clear
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <a-table
      :columns="columns"
      :data="data"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <template #tags="{ record }">
        <a-tag v-for="item of record.tags" :key="item">{{ item }}</a-tag>
      </template>
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="handleUpdate(record)">做题</a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import { useRouter } from "vue-router";
import {
  QuestionControllerService,
  QuestionQueryRequest,
  QuestionVo,
} from "@/service";
import { Message } from "@arco-design/web-vue";

const data = ref<QuestionVo[]>([]);
const router = useRouter();
const columns = [
  {
    title: "题目编号",
    dataIndex: "id",
    ellipsis: true,
  },
  {
    title: "题目",
    dataIndex: "title",
    ellipsis: true,
  },
  {
    title: "题目标签",
    slotName: "tags",
    ellipsis: true,
  },
  {
    title: "提交数",
    dataIndex: "submitNum",
  },
  {
    title: "通过数",
    dataIndex: "acceptedNum",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];
const searchParams = ref<QuestionQueryRequest>({
  pageSize: 5,
  page: 1,
  title: undefined,
  content: undefined,
  tags: [],
});
const total = ref(0);
const handleUpdate = (question: QuestionVo) => {
  router.push({
    path: "/question/view/" + question.id,
  });
};
const loadData = async () => {
  const res = await QuestionControllerService.listQuestionTinyVoByPage(
    searchParams.value
  );
  if (res.code !== 0) {
    Message.error("数据加载失败：" + res.message);
    return;
  }
  data.value = res?.data?.records ?? [];
  total.value = res.data?.total ?? 0;
};
const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    page: page,
  };
};

onMounted(() => {
  loadData();
});
watchEffect(() => {
  loadData();
});
</script>

<style scoped>
#app {
  margin: 20px 10px 0 10px;
}
</style>
